{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d267ac3b-f6ca-4f3c-af2b-0b338e8d02f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import shutil\n",
    "import re\n",
    "\n",
    "def organize_files(image_dir, non_image_dir, dup_image_dir):\n",
    "    \"\"\"\n",
    "    整理图片目录中的文件，将非图片文件和重复图片移动到指定目录。\n",
    "\n",
    "    Args:\n",
    "        image_dir (str): 图片存放目录\n",
    "        non_image_dir (str): 非图片文件输出目录\n",
    "        dup_image_dir (str): 重复图片输出目录\n",
    "    \"\"\"\n",
    "    # 创建目标目录（如果不存在）\n",
    "    os.makedirs(non_image_dir, exist_ok=True)\n",
    "    os.makedirs(dup_image_dir, exist_ok=True)\n",
    "\n",
    "    # 定义图片扩展名集合\n",
    "    image_extensions = {'.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff', '.webp'}\n",
    "\n",
    "    # 编译正则表达式匹配以(数字)结尾的主文件名\n",
    "    pattern = re.compile(r'\\(\\d+\\)$')\n",
    "\n",
    "    def get_unique_path(target_dir, filename):\n",
    "        \"\"\"生成唯一的文件名以避免冲突\"\"\"\n",
    "        base, ext = os.path.splitext(filename)\n",
    "        new_name = filename\n",
    "        counter = 1\n",
    "        # 循环直到找到不存在的文件名\n",
    "        while os.path.exists(os.path.join(target_dir, new_name)):\n",
    "            new_name = f\"{base}({counter}){ext}\"\n",
    "            counter += 1\n",
    "        return os.path.join(target_dir, new_name)\n",
    "\n",
    "    # 遍历图片目录\n",
    "    for root, _, files in os.walk(image_dir):\n",
    "        for filename in files:\n",
    "            file_path = os.path.join(root, filename)\n",
    "            \n",
    "            # 分离文件名和扩展名\n",
    "            file_base, file_ext = os.path.splitext(filename)\n",
    "            file_ext = file_ext.lower()  # 统一小写处理\n",
    "\n",
    "            # 判断文件类型\n",
    "            if file_ext not in image_extensions:\n",
    "                # 移动非图片文件\n",
    "                target_path = get_unique_path(non_image_dir, filename)\n",
    "                shutil.move(file_path, target_path)\n",
    "            else:\n",
    "                # 检查是否为重复图片\n",
    "                if pattern.search(file_base):\n",
    "                    # 移动重复图片\n",
    "                    target_path = get_unique_path(dup_image_dir, filename)\n",
    "                    shutil.move(file_path, target_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d9adc651-556b-4d08-b6a9-5459a8d4e81c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 联想小新运行\n",
    "image_dir = r'\\\\YHOME-NAS\\Private-Folder\\网络下载图片'\n",
    "non_image_dir = r'\\\\YHOME-NAS\\Private-Folder\\程序处理后输出目录\\非图片文件'\n",
    "dup_image_dir = r'\\\\YHOME-NAS\\Private-Folder\\程序处理后输出目录\\重复的图片'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bdab2b02-0fb7-4ad9-b47b-be5398281cf1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
